package cn.com.shadowless.xinning.presenter;

import java.util.HashMap;
import java.util.Map;

import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.login.LoginInfo;
import cn.com.shadowless.xinning.net.HttpMethods;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * The type Login presenter.
 *
 * @author sHadowLess
 */
public class LoginPresenter extends HttpMethods {

    private volatile static LoginPresenter instance;

    private LoginPresenter() {
    }

    /**
     * Gets singleton.
     *
     * @return the singleton
     */
    public static LoginPresenter getSingleton() {
        if (instance == null) {
            synchronized (LoginPresenter.class) {
                if (instance == null) {
                    instance = new LoginPresenter();
                }
            }
        }
        return instance;
    }

    /**
     * Gets login info.
     *
     * @param userName        the user name
     * @param pwd             the pwd
     * @param registrationId  the registration id
     * @param requestCallBack the request call back
     */
    public void getLoginInfo(String userName, String pwd, String registrationId, RequestCallBack<LoginInfo> requestCallBack) {
        Map<String, String> map = new HashMap<>();
        map.put("Account", userName);
        map.put("Password", pwd);
        map.put("DeviceType", "2");
        map.put("RegistrationID", registrationId);
        login
                .loginInfo(map)
                .compose(obsIoMain())
                .subscribe(new Observer<RequestResult<LoginInfo>>() {
                    Disposable disposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(RequestResult<LoginInfo> loginInfoRequestResult) {
                        requestCallBack.success(loginInfoRequestResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        requestCallBack.fail(exceptionMessage(checkException(e)));
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }

    /**
     * Gets logout info.
     *
     * @param userName        the user name
     * @param requestCallBack the request call back
     */
    public void getLogoutInfo(String userName, RequestCallBack<RequestResult<Object>> requestCallBack) {
        login
                .logoutInfo(userName)
                .compose(obsIoMain())
                .subscribe(new Observer<RequestResult<Object>>() {
                    Disposable disposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(RequestResult requestResult) {
                        requestCallBack.success(requestResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        requestCallBack.fail(exceptionMessage(checkException(e)));
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }
}
